package com.xshang.tenant;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;

public class TenantDataSourceBuilder {

    public static HikariDataSource build(DataSourceProperties properties) {

        HikariDataSource dataSource = new HikariDataSource();

        dataSource.setDriverClassName(properties.getDriverClassName());
        dataSource.setJdbcUrl(properties.getUrl());
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        
        // 设置数据源的更多属性，比如最大/空闲连接数，连接/空闲超时时间等
        dataSource.setMaximumPoolSize(20);
        dataSource.setMinimumIdle(5);
        dataSource.setAutoCommit(true);
        dataSource.setIdleTimeout(30000);
        dataSource.setPoolName("MyBatis-Tenant-HikariCP-Pool");
        dataSource.setConnectionTestQuery("SELECT 1");
        dataSource.setMaxLifetime(1800000);
        dataSource.setConnectionTimeout(30000);
        
        return dataSource;
    }
}